package com.herui.sdyu_lib.utils;

import android.content.SharedPreferences;

import com.herui.sdyu_lib.base.BaseAppcation;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by amanda.shan on 2016/6/22.
 */
public class StorageUtils {


    /**
     * 存储数据到 SharedPreference
     *
     * @param param
     */
    public static void saveSharedPreference(Map<String, Object> param) {//存储多个

        if (param == null || param.size() == 0)
            return;

        SharedPreferences sp = BaseAppcation.getInstance().getSharedPreferences("mlnx_param",
                BaseAppcation.getInstance().MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        for (String key : param.keySet()) {
            Object value = param.get(key);
            if (value instanceof String)
                editor.putString(key, (String) value);
            if (value instanceof Integer)
                editor.putInt(key, (Integer) value);
            if (value instanceof Boolean)
                editor.putBoolean(key, (Boolean) value);
        }
        editor.commit();
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Integer value) {//存储单个

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Boolean value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, String value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }


    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Long value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    public static String getStringSP(String key) {//得到存储，无放回空
        if (key != null) {
            SharedPreferences sp = BaseAppcation.getInstance().getSharedPreferences("mlnx_param",
                    BaseAppcation.getInstance().MODE_PRIVATE);
            return sp.getString(key, "");
        }
        return null;
    }

    public static Boolean getBooleanSP(String key) {//得到Boolean，前提是你存储的也是Boolean
        if (key != null) {
            SharedPreferences sp = BaseAppcation.getInstance().getSharedPreferences("mlnx_param",
                    BaseAppcation.getInstance().MODE_PRIVATE);
            return sp.getBoolean(key, false);
        }
        return null;
    }

    public static Integer getIntegerSP(String key) {
        if (key != null) {
            SharedPreferences sp = BaseAppcation.getInstance().getSharedPreferences("mlnx_param",
                    BaseAppcation.getInstance().MODE_PRIVATE);
            return sp.getInt(key, 0);
        }
        return null;
    }

    public static Long getLongSP(String key) {
        if (key != null) {
            SharedPreferences sp = BaseAppcation.getInstance().getSharedPreferences("mlnx_param",
                    BaseAppcation.getInstance().MODE_PRIVATE);
            return sp.getLong(key, 0);
        }
        return null;
    }

}
